package com.yuneec.droneservice.command;

import com.yuneec.droneservice.model.ControllerBattery;
import com.yuneec.droneservice.request.IResponse;
import com.yuneec.droneservice.utils.ByteUtils;

import java.util.Arrays;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetControllerBattery extends BaseCommand {
    public GetControllerBattery() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_BATTERY);
    }

    @Override
    public IResponse<ControllerBattery> toResponse(byte[] data, int off, int len) {
        int code = data[off + 1] & 0xff;
        IResponse<ControllerBattery> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            ControllerBattery controllerBattery = new ControllerBattery();
            controllerBattery.setCapacity(data[off + 2]);
            controllerBattery.setTemperature(ByteUtils.byteArrayToShort(Arrays.copyOfRange(data, off + 3, off + 5), IS_BIGENDIAN) / 10f);
            controllerBattery.setCharge(data[off + 5] == 1);
            response.setParam(controllerBattery);
        }
        return response;
    }

}
